//
//  RotationGestureViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/27.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxGesture
import RxCocoa
import RxSwift

class RotationGestureViewController: BaseViewController {

    let disposed = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tapView = UIView(frame: CGRect(x: 0, y: 80, width: 200, height: 200))
        tapView.backgroundColor = UIColor.randomColor()
        self.view.addSubview(tapView)
        
        tapView.centerInSuperView()
        
        let rotation = tapView.rx.rotationGesture().shareReplay(1)
        var rotate = 0.f
        
        
        rotation.when(.changed)
            .asRotation()
            .subscribe(onNext: { (rotation, _) in
                tapView.transform = CGAffineTransform(rotationAngle: rotation + rotate)
            })
            .disposed(by: disposed)
        
        rotation.when(.ended)
            .asRotation()
            .subscribe(onNext: { (rotation, _) in
                rotate += rotation
            })
            .disposed(by: disposed)
    }


}
